import axios from "axios"

function upload(path, userForm) {
  const params = new FormData()//因为文件上传，不能以普通方式上传，要以FormData格式上传
  for (let key in userForm) {
    if (Array.isArray(userForm[key])) {
      userForm[key].forEach((item, index) => {
        params.append(`${key}[${index}]`, item);
      });
    } else {
      params.append(key, userForm[key]);
    }
  }
  const token = localStorage.getItem('token')
  // 问题点: 因为无法覆盖封装的axios中Content-Type值,这里使用的是无封装的axios
  return axios.post(path, params, {
    headers: {
      "Content-Type": "multipart/form-data",
      "Authorization": `Bearer ${token}`
    }
  })
}

export default upload

